package com.lgk.controller;

import com.github.pagehelper.PageInfo;
import com.lgk.domain.User;
import com.lgk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 封装一个公共数据和跳转用户页
     * @param model
     * @param username
     * @return
     */
    private String getString(Model model, @PathVariable("username") String username) {
        PageInfo<User> pageUser = userService.getPageUser(1);
        model.addAttribute("page", pageUser);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", true);
        model.addAttribute("isGetUserPage", false);
        model.addAttribute("searchName","");
        return "user_list";
    }

    /**
     * 分页查询
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/getUserPage/{pageNum}/{username}", method = RequestMethod.GET)
    public String getPageUser(@PathVariable("pageNum")Integer pageNum, Model model, @PathVariable("username") String username) {
        //获取分页结果
        PageInfo<User> page = userService.getPageUser(pageNum);
        //放入请求狱中
        model.addAttribute("page", page);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", true);
        model.addAttribute("isGetUserPage", false);
        model.addAttribute("searchName","");
        return "user_list" ;
    }

    /**
     * 添加用户
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("/addUser/{username}")
    public String addUser(User user, Model model, @PathVariable("username") String username){
        Date d = new Date();
        user.setTime(d);
        userService.addUser(user);
        return getString(model, username);
    }

    /**
     * 修改用户
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("/updateUser/{username}")
    public String updateUser(Model model, User user, @PathVariable("username") String username){
        userService.updateUser(user);
        return getString(model, username);
    }

    /**
     * 根据id查询用户
     * @param id
     */
    @RequestMapping(value = "/getUser/{id}/{username}",method = RequestMethod.GET)
    public String getById(@PathVariable("username")String username, @PathVariable("id")Integer id, Model model) {
        User changeUser = userService.getById(id);
        model.addAttribute("changeUser", changeUser);
        model.addAttribute("username", username);
        return "user_update";
    }

    /**
     * 删除用户
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/deleteUser/{id}/{username}")
    public String deleteUser(@PathVariable("id")Integer id, Model model, @PathVariable("username") String username) {
        userService.deleteUser(id);
        return getString(model, username);
    }

    /**
     * 根据名字模糊分页查询
     * @param pageNum
     * @param user
     * @param model
     * @param username
     * @return
     */
    @RequestMapping("/searchUser/{username}/{pageNum}")
    public String getPageByName(@PathVariable("pageNum") Integer pageNum, User user, Model model, @PathVariable("username") String username) {
        PageInfo<User> pageUser = userService.getPageByName(user.getName(), pageNum);
        model.addAttribute("page", pageUser);
        model.addAttribute("username", username);
        model.addAttribute("isGetPage", false);
        model.addAttribute("isGetUserPage", true);
        model.addAttribute("searchName",user.getName());
        return "user_list";
    }

    /**
     * go to 用户界面
     * @param model
     * @param username
     * @return
     */
    @RequestMapping("/to/userList/{username}")
    public String toUserList(Model model, @PathVariable("username")String username){
        return getString(model, username);
    }


}
